<?php

/**
 * Paquete form base class.
 *
 * @method Paquete getObject() Returns the current form's model object
 *
 * @package    practil
 * @subpackage form
 * @author     Your name here
 */
abstract class BasePaqueteForm extends BaseFormPropel
{
  public function setup()
  {
    $this->setWidgets(array(
      'id'                                  => new sfWidgetFormInputHidden(),
      'titulo'                              => new sfWidgetFormInputText(),
      'descripcion'                         => new sfWidgetFormInputText(),
      'precio_actual'                       => new sfWidgetFormInputText(),
      'flags'                               => new sfWidgetFormInputText(),
      'codigo_twocheckout'                  => new sfWidgetFormInputText(),
      'plataforma_id'                       => new sfWidgetFormInputHidden(),
      'detalle_paquete_caracteristica_list' => new sfWidgetFormPropelChoice(array('multiple' => true, 'model' => 'PaqueteCaracteristica')),
      'usuario_paquete_list'                => new sfWidgetFormPropelChoice(array('multiple' => true, 'model' => 'Usuario')),
    ));

    $this->setValidators(array(
      'id'                                  => new sfValidatorChoice(array('choices' => array($this->getObject()->getId()), 'empty_value' => $this->getObject()->getId(), 'required' => false)),
      'titulo'                              => new sfValidatorString(array('max_length' => 255)),
      'descripcion'                         => new sfValidatorString(array('max_length' => 255)),
      'precio_actual'                       => new sfValidatorNumber(),
      'flags'                               => new sfValidatorString(array('max_length' => 20)),
      'codigo_twocheckout'                  => new sfValidatorInteger(array('min' => -2147483648, 'max' => 2147483647, 'required' => false)),
      'plataforma_id'                       => new sfValidatorPropelChoice(array('model' => 'Plataforma', 'column' => 'id', 'required' => false)),
      'detalle_paquete_caracteristica_list' => new sfValidatorPropelChoice(array('multiple' => true, 'model' => 'PaqueteCaracteristica', 'required' => false)),
      'usuario_paquete_list'                => new sfValidatorPropelChoice(array('multiple' => true, 'model' => 'Usuario', 'required' => false)),
    ));

    $this->widgetSchema->setNameFormat('paquete[%s]');

    $this->errorSchema = new sfValidatorErrorSchema($this->validatorSchema);

    parent::setup();
  }

  public function getModelName()
  {
    return 'Paquete';
  }


  public function updateDefaultsFromObject()
  {
    parent::updateDefaultsFromObject();

    if (isset($this->widgetSchema['detalle_paquete_caracteristica_list']))
    {
      $values = array();
      foreach ($this->object->getDetallePaqueteCaracteristicas() as $obj)
      {
        $values[] = $obj->getPaqueteCaracteristicaId();
      }

      $this->setDefault('detalle_paquete_caracteristica_list', $values);
    }

    if (isset($this->widgetSchema['usuario_paquete_list']))
    {
      $values = array();
      foreach ($this->object->getUsuarioPaquetes() as $obj)
      {
        $values[] = $obj->getUsuarioId();
      }

      $this->setDefault('usuario_paquete_list', $values);
    }

  }

  protected function doSave($con = null)
  {
    parent::doSave($con);

    $this->saveDetallePaqueteCaracteristicaList($con);
    $this->saveUsuarioPaqueteList($con);
  }

  public function saveDetallePaqueteCaracteristicaList($con = null)
  {
    if (!$this->isValid())
    {
      throw $this->getErrorSchema();
    }

    if (!isset($this->widgetSchema['detalle_paquete_caracteristica_list']))
    {
      // somebody has unset this widget
      return;
    }

    if (null === $con)
    {
      $con = $this->getConnection();
    }

    $c = new Criteria();
    $c->add(DetallePaqueteCaracteristicaPeer::PAQUETE_ID, $this->object->getPrimaryKey());
    DetallePaqueteCaracteristicaPeer::doDelete($c, $con);

    $values = $this->getValue('detalle_paquete_caracteristica_list');
    if (is_array($values))
    {
      foreach ($values as $value)
      {
        $obj = new DetallePaqueteCaracteristica();
        $obj->setPaqueteId($this->object->getPrimaryKey());
        $obj->setPaqueteCaracteristicaId($value);
        $obj->save();
      }
    }
  }

  public function saveUsuarioPaqueteList($con = null)
  {
    if (!$this->isValid())
    {
      throw $this->getErrorSchema();
    }

    if (!isset($this->widgetSchema['usuario_paquete_list']))
    {
      // somebody has unset this widget
      return;
    }

    if (null === $con)
    {
      $con = $this->getConnection();
    }

    $c = new Criteria();
    $c->add(UsuarioPaquetePeer::PAQUETE_ID, $this->object->getPrimaryKey());
    UsuarioPaquetePeer::doDelete($c, $con);

    $values = $this->getValue('usuario_paquete_list');
    if (is_array($values))
    {
      foreach ($values as $value)
      {
        $obj = new UsuarioPaquete();
        $obj->setPaqueteId($this->object->getPrimaryKey());
        $obj->setUsuarioId($value);
        $obj->save();
      }
    }
  }

}
